package sma.iss;

/**
 * Implementation of ISS contest with variable values.
 * 
 * @author smarsoll
 * 
 */
public class IssVariable {

	/** beta in instance. must be init with getInitialOrientation(). */
	private double beta = -2000000;

	/**
	 * This method will be called once at the beginning of each test case
	 * 
	 * @param beta
	 *            "beta angle" for this test case
	 * @return "yaw angle" which you would like to set
	 */
	public double getInitialOrientation(double beta) {
		this.beta = beta;
		return 0.0;
	}

	/**
	 * called 92 times for each test case<br>
	 * The alpha angle for that minute will be (360 degrees / 92) * minute
	 * 
	 * @param minute
	 *            values 0, 1, 2, ..., 91
	 * @return 20 elements that describe the angular position and the angular velocity of each of the 10 rotation axes
	 *         of the station at that minute
	 */
	public double[] getStateAtMinute(int minute) {
		double alphaAngle = (360.0 / 92.0) * minute;
		double nextAlpha = (360.0 / 92.0) * (minute + 1);
		if (minute == 91) {
			nextAlpha = 0.0;
		}
		double[] state = new double[20];
		// Element 0 should contain the angular position (in degrees) of the starboard SARJ.
		// state[0] = 180.0;
		state[0] = addAngle(180.0, alphaAngle);
		// state[0] = 180.0;
		// Element 1 should contain the angular velocity (in degrees per second) of the starboard SARJ.
		state[1] = addAngle(nextAlpha, -alphaAngle) / 60.0;
		// state[1] = 0.0;
		// Element 2 should contain the angular position (in degrees) of the port SARJ.
		state[2] = addAngle(0.0, -alphaAngle);
		// Element 3 should contain the angular velocity (in degrees per second) of the port SARJ.
		state[3] = 0.0;
		// Element 4 should contain the angular position (in degrees) of BGA 1A.
		// state[4] = 345.0; for beta=75
		state[4] = 340.0 + beta - 70.0;
		// Element 5 should contain the angular velocity (in degrees per second) of BGA 1A.
		state[5] = 0.0;
		// Element 6 should contain the angular position (in degrees) of BGA 2A.
		state[6] = 160.0 + beta - 70.0;
		// Element 7 should contain the angular velocity (in degrees per second) of BGA 2A.
		state[7] = 0.0;
		// Element 8 should contain the angular position (in degrees) of BGA 3A.
		state[8] = 15.0;
		// Element 9 should contain the angular velocity (in degrees per second) of BGA 3A.
		state[9] = 0.0;
		// Element 10 should contain the angular position (in degrees) of BGA 4A.
		state[10] = 195.0;
		// Element 11 should contain the angular velocity (in degrees per second) of BGA 4A.
		state[11] = 0.0;
		// Element 12 should contain the angular position (in degrees) of BGA 1B.
		state[12] = 15.0;
		// Element 13 should contain the angular velocity (in degrees per second) of BGA 1B.
		state[13] = 0.0;
		// Element 14 should contain the angular position (in degrees) of BGA 2B.
		state[14] = 195.0;
		// Element 15 should contain the angular velocity (in degrees per second) of BGA 2B.
		state[15] = 0.0;
		// Element 16 should contain the angular position (in degrees) of BGA 3B.
		state[16] = 345.0;
		// Element 17 should contain the angular velocity (in degrees per second) of BGA 3B.
		state[17] = 0.0;
		// Element 18 should contain the angular position (in degrees) of BGA 4B.
		state[18] = 165.0;
		// Element 19 should contain the angular velocity (in degrees per second) of BGA 4B.
		state[19] = 0.0;

		if (this.beta < 0) {
			// Element 4 should contain the angular position (in degrees) of BGA 1A.
			// state[4] = 198.0;
			// state[4] = 340.0 + beta - 70.0;
			// Element 5 should contain the angular velocity (in degrees per second) of BGA 1A.
			// state[5] = 0.0;
			// Element 6 should contain the angular position (in degrees) of BGA 2A.
			// state[6] = 18.0;
			// Element 7 should contain the angular velocity (in degrees per second) of BGA 2A.
			// state[7] = 0.0;
			// Element 8 should contain the angular position (in degrees) of BGA 3A.
			state[8] = 161.0;
			// Element 9 should contain the angular velocity (in degrees per second) of BGA 3A.
			state[9] = 0.0;
			// Element 10 should contain the angular position (in degrees) of BGA 4A.
			state[10] = 341.0;
			// Element 11 should contain the angular velocity (in degrees per second) of BGA 4A.
			state[11] = 0.0;
			// Element 12 should contain the angular position (in degrees) of BGA 1B.
			state[12] = 161.0;
			// Element 13 should contain the angular velocity (in degrees per second) of BGA 1B.
			state[13] = 0.0;
			// Element 14 should contain the angular position (in degrees) of BGA 2B.
			state[14] = 341.0;
			// Element 15 should contain the angular velocity (in degrees per second) of BGA 2B.
			state[15] = 0.0;
			// Element 16 should contain the angular position (in degrees) of BGA 3B.
			state[16] = 198.0;
			// Element 17 should contain the angular velocity (in degrees per second) of BGA 3B.
			state[17] = 0.0;
			// Element 18 should contain the angular position (in degrees) of BGA 4B.
			state[18] = 18.0;
			// Element 19 should contain the angular velocity (in degrees per second) of BGA 4B.
			state[19] = 0.0;
		}

		return state;
	}

	public final static double addAngle(double angle, double delta) {
		double result = angle + delta;
		if (result >= 360.0) {
			result = result - 360.0;
		} else if (result < 0.0) {
			result = result + 360.0;
		}
		return result;
	}

}
